package com.easyedu.dean.service;

import com.easyedu.core.entity.DbError;
import com.easyedu.core.entity.DeanServiceError;
import com.easyedu.core.entity.po.BuildingAddress;
import com.easyedu.core.entity.po.ResidenceHalls;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.core.dao.BuildingAddressDao;
import com.easyedu.core.dao.ResidenceHallsDao;
import com.easyedu.dean.entity.dto.request.ReqChangeBuilding;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mengranliu
 * @date 2022/3/15
 */
@Service
public class BuildingService {

     private final BuildingAddressDao buildingAddressDao;

     private final ResidenceHallsDao residenceHallsDao;

     public BuildingService(BuildingAddressDao buildingAddressDao, ResidenceHallsDao residenceHallsDao) {

          this.buildingAddressDao = buildingAddressDao;
          this.residenceHallsDao = residenceHallsDao;
     }

     public List<BuildingAddress> getAcademicBuilding() {
          return buildingAddressDao.getBuild();
     }

     public List<ResidenceHalls> getSleepBuilding() {
          return residenceHallsDao.getHalls();
     }

     public void change(ReqChangeBuilding reqChangeBuilding) {
          switch (reqChangeBuilding.getBuildingType()) {
               case Learn:
                    learnChange(reqChangeBuilding);
                    break;
               case Sleep:
                    sleepChange(reqChangeBuilding);
                    break;
          }
     }

     private void learnChange(ReqChangeBuilding reqChangeBuilding) {
          switch (reqChangeBuilding.getChangeType()) {
               case Add:
                    addLearnChange(reqChangeBuilding);
                    break;
               case Update:
                    updateLearnChange(reqChangeBuilding);
                    break;
               case Delete:
                    deleteLearnChange(reqChangeBuilding);

          }

     }

     private void sleepChange(ReqChangeBuilding reqChangeBuilding) {
          switch (reqChangeBuilding.getChangeType()) {
               case Add:
                    addSleepChange(reqChangeBuilding);
                    break;
               case Update:
                    updateSleepChange(reqChangeBuilding);
                    break;
               case Delete:
                    deleteSleepChange(reqChangeBuilding);

          }
     }

     @Transactional(rollbackFor = Exception.class)
     public void addLearnChange(ReqChangeBuilding reqChangeBuilding) {
          //检查是否存在相同的教学楼
          BuildingAddress byName = buildingAddressDao.getByName(reqChangeBuilding.getBuildName());
          if (byName != null && byName.getBuildingId() != 0) {
               throw new EasyEduException(DeanServiceError.ERR_ACADEMIC_BUILDING_EXIST);
          }
          Integer add = buildingAddressDao.add(reqChangeBuilding.getBuildName(), reqChangeBuilding.getX(),
                  reqChangeBuilding.getY());
          if (add != 1) {
               throw new EasyEduException(DbError.Err_INSERT_DB_ERROR);
          }
     }

     @Transactional(rollbackFor = Exception.class)
     public void updateLearnChange(ReqChangeBuilding reqChangeBuilding) {
          BuildingAddress byId = buildingAddressDao.getById(reqChangeBuilding.getBuildingId());
          if (byId == null || byId.getBuildingId() == 0) {
               throw new EasyEduException(DeanServiceError.ERR_ACADEMIC_BUILDING_NOT_EXIST);
          }
          Integer integer =
                  buildingAddressDao.updateById(reqChangeBuilding.getBuildingId(), reqChangeBuilding.getBuildName(),
                          reqChangeBuilding.getX(), reqChangeBuilding.getY());
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_UPDATE_DB_ERROR);
          }
     }

     private void deleteLearnChange(ReqChangeBuilding reqChangeBuilding) {
          //检查是否存在相同的宿舍楼
          BuildingAddress byName = buildingAddressDao.getByName(reqChangeBuilding.getBuildName());
          if (byName == null || byName.getBuildingId() == 0) {
               throw new EasyEduException(DeanServiceError.ERR_ACADEMIC_BUILDING_NOT_EXIST);
          }
          Integer integer = buildingAddressDao.deleteById(reqChangeBuilding.getBuildingId());
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_DELETE_DB_ERROR);
          }
     }

     @Transactional(rollbackFor = Exception.class)
     public void addSleepChange(ReqChangeBuilding reqChangeBuilding) {
          //检查是否存在相同的宿舍楼
          ResidenceHalls byName = residenceHallsDao.getByName(reqChangeBuilding.getBuildName());
          if (byName != null && byName.getHallsId() != 0) {
               throw new EasyEduException(DeanServiceError.ERR_RESIDENCE_HALLS_EXIST);
          }
          Integer add = residenceHallsDao.add(reqChangeBuilding.getBuildName(), reqChangeBuilding.getX(),
                  reqChangeBuilding.getY());
          if (add != 1) {
               throw new EasyEduException(DbError.Err_INSERT_DB_ERROR);
          }
     }

     @Transactional(rollbackFor = Exception.class)
     public void updateSleepChange(ReqChangeBuilding reqChangeBuilding) {
          ResidenceHalls byId = residenceHallsDao.getById(reqChangeBuilding.getBuildingId());
          if (byId == null || byId.getHallsId() == 0) {
               throw new EasyEduException(DeanServiceError.ERR_RESIDENCE_HALLS_NOT_EXIST);
          }
          Integer integer =
                  residenceHallsDao.updateById(reqChangeBuilding.getBuildingId(), reqChangeBuilding.getBuildName(),
                          reqChangeBuilding.getX(), reqChangeBuilding.getY());
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_UPDATE_DB_ERROR);
          }
     }

     private void deleteSleepChange(ReqChangeBuilding reqChangeBuilding) {
          //检查是否存在相同的宿舍楼
          ResidenceHalls byName = residenceHallsDao.getByName(reqChangeBuilding.getBuildName());
          if (byName == null || byName.getHallsId() == 0) {
               throw new EasyEduException(DeanServiceError.ERR_RESIDENCE_HALLS_NOT_EXIST);
          }
          Integer integer = residenceHallsDao.deleteById(reqChangeBuilding.getBuildingId());
          if (integer != 1) {
               throw new EasyEduException(DbError.Err_DELETE_DB_ERROR);
          }
     }
}
